﻿using DirectShowLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlayCap.Wpf
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        #region Properties

        public CameraCapturer Capturer { get; set; } 

        #endregion

        public MainWindow()
        {
            InitializeComponent();

            this.Closing += (sender, e) =>
            {
                if (this.Capturer != null)
                {
                    this.Capturer.CloseInterfaces();
                }
            };

            this.Loaded += (sender, e) =>
            {
                HwndSource hs = (HwndSource)PresentationSource.FromDependencyObject(this.ui_grid);
                IntPtr _handle = hs.Handle;

                this.Capturer = new CameraCapturer
                {
                    ContainerHandle = _handle,
                    VideoSize = new Size(this.ui_grid.ActualWidth, this.ui_grid.ActualHeight)
                };
            };

        }


        #region Functions.Button

        private void OnStartClick(object sender, RoutedEventArgs e)
        {
            this.Capturer.CaptureVideo();
        }

        private void OnRotateClick(object sender, RoutedEventArgs e)
        {

            ////this.Capturer.CaptureVideo();
            //this.Capturer.RotateVideoWindow();
        }

        private void OnGetResolutionsClick(object sender, RoutedEventArgs e)
        {
            var _dict = this.Capturer.GetResolutionDict();
            if (_dict==null) { MessageBox.Show("Error for get resolutions");return; }

            var _string = new StringBuilder();

            foreach (var x in _dict)
            {
                _string.AppendLine(x.Key);
            }
            MessageBox.Show(_string.ToString());
        }

        #endregion

    }
}
